# -*- coding: utf-8 -*-
"""
自定义SpinBox控件
支持多种风格和明暗主题
"""

from enum import Enum
from PySide2.QtWidgets import QSpinBox
from PySide2.QtCore import Qt


class ThemeMode(Enum):
    """主题模式枚举"""
    LIGHT = "light"
    DARK = "dark"


class SpinBoxStyle(Enum):
    """SpinBox风格枚举"""
    DEFAULT = "default"
    MODERN = "modern"
    ROUNDED = "rounded"
    MATERIAL = "material"


class CustomSpinBox(QSpinBox):
    """自定义SpinBox控件"""
    
    def __init__(self, theme=ThemeMode.LIGHT, style=SpinBoxStyle.DEFAULT, parent=None):
        super().__init__(parent)
        
        # 处理主题参数类型
        if isinstance(theme, str):
            self.theme = ThemeMode.DARK if theme.lower() == "dark" else ThemeMode.LIGHT
        else:
            self.theme = theme
            
        self.style = style
        self.set_theme(self.theme)
    
    def set_theme(self, theme):
        """设置主题"""
        self.theme = theme
        self._apply_style()
    
    def set_style(self, style):
        """设置风格"""
        self.style = style
        self._apply_style()
    
    def update_theme(self, theme_name):
        """更新主题（兼容字符串参数）"""
        if isinstance(theme_name, str):
            theme = ThemeMode.DARK if theme_name.lower() == "dark" else ThemeMode.LIGHT
        else:
            theme = theme_name
        self.set_theme(theme)
    
    def _apply_style(self):
        """应用样式"""
        if self.style == SpinBoxStyle.DEFAULT:
            stylesheet = self._get_default_style()
        elif self.style == SpinBoxStyle.MODERN:
            stylesheet = self._get_modern_style()
        elif self.style == SpinBoxStyle.ROUNDED:
            stylesheet = self._get_rounded_style()
        elif self.style == SpinBoxStyle.MATERIAL:
            stylesheet = self._get_material_style()
        else:
            stylesheet = self._get_default_style()
        
        self.setStyleSheet(stylesheet)
    
    def _get_default_style(self):
        """获取默认风格样式"""
        if self.theme == ThemeMode.DARK:
            return """
                QSpinBox {
                    border: 1px solid #4a5568;
                    border-radius: 4px;
                    padding: 5px;
                    background-color: #2d3748;
                    color: #e2e8f0;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #374151;
                }
                
                QSpinBox:disabled {
                    background-color: #1a202c;
                    color: #718096;
                    border-color: #2d3748;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 20px;
                    border-left: 1px solid #4a5568;
                    border-bottom: 1px solid #4a5568;
                    background-color: #4a5568;
                    border-top-right-radius: 3px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #5a6c7d;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #718096;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 20px;
                    border-left: 1px solid #4a5568;
                    background-color: #4a5568;
                    border-bottom-right-radius: 3px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #5a6c7d;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #718096;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 12px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 12px;
                    height: 6px;
                }
            """
        else:
            return """
                QSpinBox {
                    border: 1px solid #bdc3c7;
                    border-radius: 4px;
                    padding: 5px;
                    background-color: #ffffff;
                    color: #2c3e50;
                    font-size: 15px;
                    min-height: 20px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #f8f9fa;
                }
                
                QSpinBox:disabled {
                    background-color: #ecf0f1;
                    color: #95a5a6;
                    border-color: #d5dbdb;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 20px;
                    border-left: 1px solid #bdc3c7;
                    border-bottom: 1px solid #bdc3c7;
                    background-color: #ecf0f1;
                    border-top-right-radius: 3px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #bdc3c7;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #95a5a6;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 20px;
                    border-left: 1px solid #bdc3c7;
                    background-color: #ecf0f1;
                    border-bottom-right-radius: 3px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #bdc3c7;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #95a5a6;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 12px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 12px;
                    height: 6px;
                }
            """
    
    def _get_modern_style(self):
        """获取现代风格样式"""
        if self.theme == ThemeMode.DARK:
            return """
                QSpinBox {
                    border: 2px solid #4a5568;
                    border-radius: 8px;
                    padding: 8px 12px;
                    background-color: #1a202c;
                    color: #f7fafc;
                    font-size: 15px;
                    font-weight: 500;
                    min-height: 24px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #2d3748;
                }
                
                QSpinBox:disabled {
                    background-color: #0d1117;
                    color: #6b7280;
                    border-color: #374151;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 24px;
                    border-left: 2px solid #4a5568;
                    border-bottom: 1px solid #4a5568;
                    background-color: #374151;
                    border-top-right-radius: 6px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #4b5563;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #6b7280;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 24px;
                    border-left: 2px solid #4a5568;
                    background-color: #374151;
                    border-bottom-right-radius: 6px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #4b5563;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #6b7280;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 14px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 14px;
                    height: 6px;
                }
            """
        else:
            return """
                QSpinBox {
                    border: 2px solid #e5e7eb;
                    border-radius: 8px;
                    padding: 8px 12px;
                    background-color: #ffffff;
                    color: #111827;
                    font-size: 15px;
                    font-weight: 500;
                    min-height: 24px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #f9fafb;
                }
                
                QSpinBox:disabled {
                    background-color: #f3f4f6;
                    color: #9ca3af;
                    border-color: #d1d5db;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 24px;
                    border-left: 2px solid #e5e7eb;
                    border-bottom: 1px solid #e5e7eb;
                    background-color: #f3f4f6;
                    border-top-right-radius: 6px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #e5e7eb;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #d1d5db;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 24px;
                    border-left: 2px solid #e5e7eb;
                    background-color: #f3f4f6;
                    border-bottom-right-radius: 6px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #e5e7eb;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #d1d5db;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 14px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 14px;
                    height: 6px;
                }
            """
    
    def _get_rounded_style(self):
        """获取圆角风格样式"""
        if self.theme == ThemeMode.DARK:
            return """
                QSpinBox {
                    border: 1px solid #4a5568;
                    border-radius: 15px;
                    padding: 6px 15px;
                    background-color: #2d3748;
                    color: #e2e8f0;
                    font-size: 15px;
                    min-height: 22px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #374151;
                }
                
                QSpinBox:disabled {
                    background-color: #1a202c;
                    color: #718096;
                    border-color: #2d3748;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 22px;
                    border-left: 1px solid #4a5568;
                    border-bottom: 1px solid #4a5568;
                    background-color: #4a5568;
                    border-top-right-radius: 14px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #5a6c7d;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #718096;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 22px;
                    border-left: 1px solid #4a5568;
                    background-color: #4a5568;
                    border-bottom-right-radius: 14px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #5a6c7d;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #718096;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 12px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 12px;
                    height: 6px;
                }
            """
        else:
            return """
                QSpinBox {
                    border: 1px solid #bdc3c7;
                    border-radius: 15px;
                    padding: 6px 15px;
                    background-color: #ffffff;
                    color: #2c3e50;
                    font-size: 15px;
                    min-height: 22px;
                }
                
                QSpinBox:focus {
                    border-color: #3498db;
                    background-color: #f8f9fa;
                }
                
                QSpinBox:disabled {
                    background-color: #ecf0f1;
                    color: #95a5a6;
                    border-color: #d5dbdb;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 22px;
                    border-left: 1px solid #bdc3c7;
                    border-bottom: 1px solid #bdc3c7;
                    background-color: #ecf0f1;
                    border-top-right-radius: 14px;
                }
                
                QSpinBox::up-button:hover {
                    background-color: #bdc3c7;
                }
                
                QSpinBox::up-button:pressed {
                    background-color: #95a5a6;
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 22px;
                    border-left: 1px solid #bdc3c7;
                    background-color: #ecf0f1;
                    border-bottom-right-radius: 14px;
                }
                
                QSpinBox::down-button:hover {
                    background-color: #bdc3c7;
                }
                
                QSpinBox::down-button:pressed {
                    background-color: #95a5a6;
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 12px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 12px;
                    height: 6px;
                }
            """
    
    def _get_material_style(self):
        """获取Material Design风格样式"""
        if self.theme == ThemeMode.DARK:
            return """
                QSpinBox {
                    border: none;
                    border-bottom: 2px solid #4a5568;
                    border-radius: 0px;
                    padding: 8px 5px;
                    background-color: transparent;
                    color: #e2e8f0;
                    font-size: 15px;
                    min-height: 24px;
                }
                
                QSpinBox:focus {
                    border-bottom-color: #3498db;
                    background-color: rgba(52, 152, 219, 0.05);
                }
                
                QSpinBox:disabled {
                    background-color: transparent;
                    color: #718096;
                    border-bottom-color: #2d3748;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QSpinBox::up-button:hover {
                    background-color: rgba(74, 85, 104, 0.3);
                }
                
                QSpinBox::up-button:pressed {
                    background-color: rgba(74, 85, 104, 0.5);
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QSpinBox::down-button:hover {
                    background-color: rgba(74, 85, 104, 0.3);
                }
                
                QSpinBox::down-button:pressed {
                    background-color: rgba(74, 85, 104, 0.5);
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 14px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 14px;
                    height: 6px;
                }
            """
        else:
            return """
                QSpinBox {
                    border: none;
                    border-bottom: 2px solid #bdc3c7;
                    border-radius: 0px;
                    padding: 8px 5px;
                    background-color: transparent;
                    color: #2c3e50;
                    font-size: 15px;
                    min-height: 24px;
                }
                
                QSpinBox:focus {
                    border-bottom-color: #3498db;
                    background-color: rgba(52, 152, 219, 0.05);
                }
                
                QSpinBox:disabled {
                    background-color: transparent;
                    color: #95a5a6;
                    border-bottom-color: #d5dbdb;
                }
                
                QSpinBox::up-button {
                    subcontrol-origin: border;
                    subcontrol-position: top right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QSpinBox::up-button:hover {
                    background-color: rgba(189, 195, 199, 0.3);
                }
                
                QSpinBox::up-button:pressed {
                    background-color: rgba(189, 195, 199, 0.5);
                }
                
                QSpinBox::down-button {
                    subcontrol-origin: border;
                    subcontrol-position: bottom right;
                    width: 20px;
                    border: none;
                    background-color: transparent;
                }
                
                QSpinBox::down-button:hover {
                    background-color: rgba(189, 195, 199, 0.3);
                }
                
                QSpinBox::down-button:pressed {
                    background-color: rgba(189, 195, 199, 0.5);
                }
                
                QSpinBox::up-arrow {
                    image: url(:/mqss/up.svg);
                    width: 14px;
                    height: 6px;
                }
                
                QSpinBox::down-arrow {
                    image: url(:/mqss/down.svg);
                    width: 14px;
                    height: 6px;
                }
            """